<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
       <el-form-item label="区域" prop="deptId">
        <select-input dsCode="sys_dept_community" v-model="queryParams.deptId" style="width:180px"/>
      </el-form-item>
      <el-form-item label="房屋" prop="roomId">
        <el-input v-model="queryRoomName" placeholder="请选择" readonly @click="doSelectRoom('query')" />
        <el-input v-model="queryParams.roomId" type="hidden" />
      </el-form-item>
      <el-form-item label="装修日期" style="width: 308px">
        <el-date-picker
          v-model="daterangeStartDate"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="进度" prop="progress">
        <el-select v-model="queryParams.progress" placeholder="请选择进度" clearable style="width:120px">
          <el-option
            v-for="dict in project_progress"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="缴费状态" prop="chargeStatus">
        <el-select v-model="queryParams.chargeStatus" placeholder="请选择" clearable style="width:120px">
          <el-option
            v-for="dict in charge_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['service:fitment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['service:fitment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['service:fitment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fitmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="区域" align="center" prop="deptName" />
      <el-table-column label="楼栋" align="center" prop="buildingName" />
      <el-table-column label="单元号" align="center" prop="unitNum" />
      <el-table-column label="房屋号" align="center" prop="roomName" />
      <el-table-column label="业主姓名" align="center" prop="personName" />
      <el-table-column label="开始日期" align="center" prop="startDate" width="180" />
      <el-table-column label="结束日期" align="center" prop="endDate" width="180" />
      <el-table-column label="装修前照片" align="center" prop="imgBefore" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.imgBefore" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="装修后照片" align="center" prop="imgAfter" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.imgAfter" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="装修说明" align="center" prop="remark" />
      <el-table-column label="进度" align="center" prop="progress">
        <template #default="scope">
          <dict-tag :options="project_progress" :value="scope.row.progress"/>
        </template>
      </el-table-column>
      <el-table-column label="押金" align="center" prop="feeDeposit" />
      <el-table-column label="缴费状态" align="center" prop="chargeStatus">
        <template #default="scope">
          <dict-tag :options="charge_status" :value="scope.row.chargeStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['service:fitment:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['service:fitment:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改装修信息对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="fitmentRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="房屋" prop="roomId">
          <el-input v-model="formRoomName" placeholder="请选择" readonly @click="doSelectRoom('form')" />
          <el-input v-model="form.roomId" type="hidden" />
        </el-form-item>
        <el-form-item label="开始日期" prop="startDate">
          <el-date-picker clearable
            v-model="form.startDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endDate">
          <el-date-picker clearable
            v-model="form.endDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="装修前照片" prop="imgBefore">
          <image-upload v-model="form.imgBefore"/>
        </el-form-item>
        <el-form-item label="装修说明" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入装修说明" />
        </el-form-item>
        <el-form-item label="押金" prop="feeDeposit">
          <el-input-number v-model="form.feeDeposit" placeholder="请输入押金" />
        </el-form-item>
        <el-form-item label="缴费状态" prop="chargeStatus">
          <el-select v-model="form.chargeStatus" placeholder="请选择" clearable style="width:120px">
            <el-option
              v-for="dict in charge_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <template v-if="form.id">
        <el-form-item label="进度" prop="progress">
          <el-select v-model="form.progress" placeholder="请选择进度">
            <el-option
              v-for="dict in project_progress"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="装修后照片" prop="imgAfter">
          <image-upload v-model="form.imgAfter"/>
        </el-form-item>
        </template>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <room-select :show="showRoomSelect" :params="roomSelectParams" @select="selectRoom"/>
  </div>
</template>

<script setup name="Fitment">
import { listFitment, getFitment, delFitment, addFitment, updateFitment } from "@/api/service/fitment";
import SelectInput from "@/components/FormInput/Select";
import RoomSelect from "@/components/Selection/Room";

const { proxy } = getCurrentInstance();
const { sys_yes_no, project_progress, charge_status } = proxy.useDict('sys_yes_no', 'project_progress', 'charge_status');

const fitmentList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeStartDate = ref([]);
const queryRoomName = ref("");
const formRoomName = ref("");
const showRoomSelect = ref(false);
const roomSelectParams = ref(null);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 't.id',
    isAsc: 'desc',
    deptId: null,
    roomId: null,
    startDate: null,
    endDate: null,
    progress: null,
    chargeStatus: null,
  },
  rules: {
    deptId: [
      { required: true, message: "区域不能为空", trigger: "blur" }
    ],
    roomId: [
      { required: true, message: "房屋不能为空", trigger: "blur" }
    ],
    feeDeposit: [
      { required: true, message: "押金不能为空", trigger: "blur" }
    ],
    chargeStatus: [
      { required: true, message: "缴费状态不能为空", trigger: "change" }
    ],
    startDate: [
      { required: true, message: "开始日期不能为空", trigger: "blur" }
    ],
    endDate: [
      { required: true, message: "结束日期不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询装修信息列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeStartDate && '' != daterangeStartDate) {
    queryParams.value.params["beginStartDate"] = daterangeStartDate.value[0];
    queryParams.value.params["endStartDate"] = daterangeStartDate.value[1];
  }
  listFitment(queryParams.value).then(response => {
    fitmentList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function doSelectRoom(params){
  roomSelectParams.value = params;
  showRoomSelect.value = true;
}
function selectRoom(room){
  showRoomSelect.value = false;
  if(room == null){
    form.value.roomId = null;
    formRoomName.value = null;
    queryParams.value.roomId = null;
    queryRoomName.value = null;
    return;
  }
  if(room.params == 'form'){
    form.value.roomId = room.id;
    formRoomName.value = room.name;
  }else{
    queryParams.value.roomId = room.id;
    queryRoomName.value = room.name;
  }
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    deptId: null,
    roomId: null,
    feeDeposit: null,
    startDate: null,
    endDate: null,
    imgBefore: null,
    imgAfter: null,
    remark: null,
    progress: '0',
    chargeStatus: '0',
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("fitmentRef");
  formRoomName.value = null;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryRoomName.value = null;
  daterangeStartDate.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加装修信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getFitment(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改装修信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["fitmentRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateFitment(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFitment(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除选中的装修信息数据项？').then(function() {
    return delFitment(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('service/fitment/export', {
    ...queryParams.value
  }, `fitment_${new Date().getTime()}.xlsx`)
}

/** 导入按钮操作 */
function handleImport() {
  upload.title = "装修信息导入";
  upload.open = true;
};

/** 下载模板操作 */
function importTemplate() {
  proxy.download("service/fitment/importTemplate", {
  }, `fitment_template.xlsx`);
};

/**文件上传中处理 */
const handleFileUploadProgress = (event, file, fileList) => {
  upload.isUploading = true;
};

/** 文件上传成功处理 */
const handleFileSuccess = (response, file, fileList) => {
  upload.open = false;
  upload.isUploading = false;
  proxy.$refs["uploadRef"].handleRemove(file);
  proxy.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
  getList();
};

/** 提交上传文件 */
function submitFileForm() {
  proxy.$refs["uploadRef"].submit();
};

getList();
</script>
